package com.kinglong.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.kinglong.demo.model.User;
import com.kinglong.demo.model.UserVo;
import com.kinglong.demo.service.UserService;

/**
 * @author jyangzi5
 * @version 1.0
 * @description: TODO
 * @date 2021/4/19 11:26
 */
@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;

	/**
	 * 新增用户
	 * 
	 * @param user
	 * @return
	 */
	int age = 1;

	@RequestMapping("/add")
	public Object add() {
		User user = new User();
		user.setId(System.currentTimeMillis());
		user.setAge(++age);
		user.setName("Andy-" + age);
		Map<String, Object> result = new HashMap<String, Object>(16);
		userService.saveUser(user);
		result.put("code", 200);
		result.put("msg", "保存信息成功");
		return result;
	}

	/**
	 * 删除信息，支持批量删除
	 *
	 * @param ids
	 * @return
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Object del(String ids) {
		Map<String, Object> result = new HashMap<String, Object>(16);
		userService.deleteUser(ids.split(","));
		result.put("code", 200);
		result.put("msg", "删除成功");
		return result;
	}

	/**
	 * @param user
	 * @return
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public Object modify(User user) {
		Map<String, Object> result = new HashMap<String, Object>();
		userService.modifyUser(user);
		result.put("code", 200);
		result.put("msg", "更新信息成功");
		return result;
	}

	/**
	 * @param userVo
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Object list(UserVo userVo) {
		List<User> userList = userService.getUserList(userVo);
		// int count = userService.getUserCount(userVo);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", 200);
		// result.put("count", count);
		result.put("data", userList);
		return result;
	}

	/**
	 * 编辑
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/get")
	@ResponseBody
	public Object get(Long id) {
		User user = userService.getUserById(id);
		return user;
	}
}
